package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.TeacherScore;
import cn.ztydata.teachingsystem.heaven.service.TeacherScoreService;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherScoreDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.Map;

/**
 * 教师评分控制器
 * Created by wyh on 2015/3/20.
 */


@Controller
@RequestMapping("/score")
public class TeacherScoreController extends BaseController {
    @Autowired
    private TeacherScoreService teacherScoreService;

    /**
     * 新增教师建议
     * @param dto 建议dto
     * @return JsonResult
     *
     * @author wyh
     * @since 2015-03-26
     */
    @RequestMapping(value = "/lessonAdd", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult saveScore(@RequestBody @Valid TeacherScoreDto dto){
        Assert.notNull(dto, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "教师评分dto为空"));

        teacherScoreService.saveScore(mapper.map(dto, TeacherScore.class));

        log.debug("返回 新增教师评分成功");
        return new JsonResult(true, "新增成功");
    }

    /**
     * 评分预览
     * @param groupId 群组ID
     * @param chapterId 章ID
     * @param studentId 学生ID
     * @return json
     *
     * @author wyh
     * @since 2015-03-26
     */
    @RequestMapping(value = "/lessonPreview", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData<Map<String, Object>> getScore(@RequestParam("groupId") long groupId,
                                                        @RequestParam("chapterId") long chapterId,
                                                        @RequestParam("studentId") long studentId){
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.isTrue(chapterId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "章ID"));
        Assert.isTrue(studentId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "学生ID"));

        log.debug("返回 教师评分预览");
        return new JsonResultData<>(teacherScoreService.getScore(groupId, chapterId, studentId));
    }
}
